﻿//[lzh]
using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class TestUGUI : MonoBehaviour
{
    public Transform target;
    public Camera m_camera;
    public Button btn_reset;

    // 初始状态
    private Vector3 initPosition = Vector3.zero;
    private Quaternion initRotation = Quaternion.identity;
    private Vector3 initLocalScale = Vector3.one;

    // 手势的一些状态数据
    private Vector3 deltaPosition_for_move = Vector3.zero;
    private Vector2 deltaPosition_for_rotate = Vector2.zero;
    private Quaternion lastRotation_for_rotate = Quaternion.identity;

    // 用于控制单与双指的冲突
    bool canSingleFinger = true;
    bool startSingFinger = false;

    void Start()
    {
        if (target)
        {
            initPosition = target.position;
            initRotation = target.rotation;
            initLocalScale = target.localScale;
        }

        btn_reset.onClick.AddListener(() =>
        {
            target.position = initPosition;
            target.rotation = initRotation;
            target.localScale = initLocalScale;

            deltaPosition_for_move = initPosition;
            deltaPosition_for_rotate = initPosition;
            lastRotation_for_rotate = initRotation;
        });
    }

    void OnEnable()
    {
        EasyTouch.On_TouchStart2Fingers += On_TouchStart2Fingers;
        EasyTouch.On_Cancel2Fingers += On_Cancel2Fingers;

        // 单指转动
        EasyTouch.On_TouchStart += On_TouchStart;
        EasyTouch.On_Drag += EasyTouch_On_Drag;
        EasyTouch.On_DragEnd += EasyTouch_On_DragEnd;

        // 双指移动
        EasyTouch.On_DragStart2Fingers += On_DragStart2Fingers;
        EasyTouch.On_Drag2Fingers += On_Drag2Fingers;
        EasyTouch.On_DragEnd2Fingers += On_DragEnd2Fingers;

        // 双指放缩
        EasyTouch.On_PinchIn += On_PinchIn;
        EasyTouch.On_PinchOut += On_PinchOut;
        EasyTouch.On_PinchEnd += On_PinchEnd;
    }

    void OnDisable()
    {
        EasyTouch.On_TouchStart2Fingers -= On_TouchStart2Fingers;
        EasyTouch.On_Cancel2Fingers -= On_Cancel2Fingers;

        EasyTouch.On_TouchStart -= On_TouchStart;
        EasyTouch.On_Drag -= EasyTouch_On_Drag;
        EasyTouch.On_DragEnd -= EasyTouch_On_DragEnd;

        EasyTouch.On_DragStart2Fingers -= On_DragStart2Fingers;
        EasyTouch.On_Drag2Fingers -= On_Drag2Fingers;
        EasyTouch.On_DragEnd2Fingers -= On_DragEnd2Fingers;

        EasyTouch.On_PinchIn -= On_PinchIn;
        EasyTouch.On_PinchOut -= On_PinchOut;
        EasyTouch.On_PinchEnd -= On_PinchEnd;
    }
   
    private void On_TouchStart2Fingers(Gesture gesture)
    {
        canSingleFinger = false;
        startSingFinger = false;
    }

    private void On_Cancel2Fingers(Gesture gesture)
    {
        canSingleFinger = true;
    }
    

    //================================================

    public void On_TouchStart(Gesture gesture)
    {
        Debug.Log("gesture.touchCount = " + gesture.touchCount);
        Debug.Log("gesture.fingerIndex = " + gesture.fingerIndex);
        if (gesture.touchCount > 1) return;
        if (!canSingleFinger) return;
        
        Debug.Log("Touch in " + gesture.position);
        startSingFinger = true;

        deltaPosition_for_rotate = gesture.position;
        lastRotation_for_rotate = target.rotation;

    }

    private void EasyTouch_On_Drag(Gesture gesture)
    {
        if (gesture.touchCount > 1) return;
        if (!canSingleFinger || !startSingFinger)
        {
            if (canSingleFinger == true && startSingFinger == false)
            {
                On_TouchStart(gesture);
            }
            return;
        }
        Debug.Log("EasyTouch_On_Drag " + gesture.position);

        Vector2 offset = gesture.position - deltaPosition_for_rotate;
        offset = new Vector2(offset.x * 0.3f, offset.y * 0.3f);
        if (Mathf.Abs(offset.x) > Mathf.Abs(offset.y))
        {
            offset.y = 0f;
        }
        else
        {
            offset.x = 0f;
        }

        target.rotation = lastRotation_for_rotate;
        target.Rotate(new Vector3(offset.y, -offset.x,0f), Space.World);

        deltaPosition_for_rotate = gesture.position;
        lastRotation_for_rotate = target.rotation;
    }

    private void EasyTouch_On_DragEnd(Gesture gesture)
    {
        if (gesture.touchCount > 1) return;
        if (!canSingleFinger || !startSingFinger) return;
        startSingFinger = false;
    }

    //================================================

    //public void On_TouchStart(Gesture gesture)
    //{
    //    Debug.Log("Touch in " + gesture.position);       

    //    Vector3 position = m_camera.ScreenToWorldPoint(new Vector3(gesture.position.x, gesture.position.y, target.position.z));
    //    deltaPosition_for_move = target.position - position;

    //}

    //private void EasyTouch_On_Drag(Gesture gesture)
    //{
    //    Debug.Log("EasyTouch_On_Drag " + gesture.position);
    //    Vector3 position = m_camera.ScreenToWorldPoint(new Vector3(gesture.position.x, gesture.position.y, target.position.z));
    //    target.position = position + deltaPosition_for_move;
    //}

    void On_DragStart2Fingers(Gesture gesture)
    {
        Vector3 position = m_camera.ScreenToWorldPoint(new Vector3(gesture.position.x, gesture.position.y, target.position.z));
        deltaPosition_for_move = target.position - position;
    }

    void On_Drag2Fingers(Gesture gesture)
    {
        Vector3 position = m_camera.ScreenToWorldPoint(new Vector3(gesture.position.x, gesture.position.y, target.position.z));
        target.position = position + deltaPosition_for_move;
    }

    void On_DragEnd2Fingers(Gesture gesture)
    {
        deltaPosition_for_move = Vector3.zero;
    }

    //================================================
    private void On_PinchIn(Gesture gesture)
    {
        float zoom = Time.deltaTime * gesture.deltaPinch * 0.3f;
        
        Vector3 scale = target.localScale;
        if (scale.x - zoom < 0.3)
        {
            target.localScale = Vector3.one * 0.3f;
        }
        else
        {
            target.localScale = new Vector3(scale.x - zoom, scale.y - zoom, scale.z - zoom);
        }
    }

    private void On_PinchOut(Gesture gesture)
    {
        float zoom = Time.deltaTime * gesture.deltaPinch * 0.3f;
        Vector3 scale = target.localScale;
        if (scale.x + zoom > 2)
        {
            target.localScale = Vector3.one * 2f;
        }
        {
            target.localScale = new Vector3(scale.x + zoom, scale.y + zoom, scale.z + zoom);
        }
    }

    private void On_PinchEnd(Gesture gesture)
    {

    }
}
